package vloc

import (
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"github.com/volcengine/volc-sdk-golang/base"
	"net/http"
	"net/url"
	"time"
)

var (
	ServiceInfo = &base.ServiceInfo{
		Timeout: 5 * time.Second,
		Host:    "translate.volcengineapi.com",
		Header: http.Header{
			"Accept": []string{"application/json"},
		},
		Credentials: base.Credentials{Region: base.RegionCnNorth1, Service: "translate"},
	}
	ApiInfoList = map[string]*base.ApiInfo{
		"TranslateText": {
			Method: http.MethodPost,
			Path:   "/",
			Query: url.Values{
				"Action":  []string{"TranslateText"},
				"Version": []string{"2020-06-01"},
			},
		},
	}
	ViperForTranslate = viper.New()

	accessKey string
	secretKey string
)

func init() {
	ViperForTranslate.AddConfigPath(".")
	ViperForTranslate.SetConfigName("application")
	ViperForTranslate.SetConfigType("yaml")
	if err := ViperForTranslate.ReadInConfig(); err != nil {
		logrus.Errorf("[translation]-读取./application.yaml异常, err=%s", err)
		return
	}
	if !ViperForTranslate.IsSet("accessKey") || !ViperForTranslate.IsSet("secretKey") {
		logrus.Errorf("[TranslateClient]-未从application.yaml中获取到accessKey和secretKey")
		return
	}
	accessKey = ViperForTranslate.GetString("accessKey")
	secretKey = ViperForTranslate.GetString("secretKey")
}

func TranslateClient() (client *base.Client) {
	client = base.NewClient(ServiceInfo, ApiInfoList)
	client.SetAccessKey(accessKey)
	client.SetSecretKey(secretKey)
	return
}

type Req struct {
	SourceLanguage string   `json:"SourceLanguage,omitempty"`
	TargetLanguage string   `json:"TargetLanguage" binding:"required"`
	TextList       []string `json:"TextList,omitempty"`
}
